//
//  InterestRateTableViewCell.swift
//  koolcar
//
//  Created by kuaimaxiansheng on 2022/5/16.
//

import UIKit

protocol InterestRateTableViewCellDelegate {
    
    func selectInterestRate(rate: Double)
}

class InterestRateTableViewCell: UITableViewCell {
    var leftLab: UILabel!
    var questionBtn: UIButton!
    var rigthLab: UILabel!
    var line: UIView!
    var textField: UITextField!
    var subtractBtn: UIButton!
    var addBtn: UIButton!
    var delegate: InterestRateTableViewCellDelegate!
    var contentModel: CarDetailsContentModel!
    override init(style: UITableViewCellStyle, reuseIdentifier: String?){
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.backgroundColor = .white
        self.selectionStyle = .none
        self.clipsToBounds = true
        self.initSubview()
        self.addLayout()
    }
    
    func initSubview() {
        
        self.leftLab = UILabel(frame: CGRect(x: 18, y: 0, width: 100, height: 52))
        self.leftLab.font = UIFont.init(name: Theme.PingFangSCMedium, size: 14)
        self.leftLab.textColor = Theme.titleLabColor
        self.contentView.addSubview(self.leftLab)
        
        
        self.questionBtn = UIButton(frame: CGRect(x: 0, y: 0, width: 14, height: 14))
        self.questionBtn.setImage(UIImage(named: "icon_question_mark"), for: .normal)
        self.questionBtn.addTarget(self, action: #selector(questionBtnAntion), for: .touchUpInside)
        self.questionBtn.contentHorizontalAlignment = .left
        self.contentView.addSubview(self.questionBtn)
        
        
        self.textField = UITextField.init(frame: .zero)
        self.textField.textColor = Theme.titleLabColor
        self.textField.font = UIFont.init(name: Theme.PingFangSCMedium, size: 14)
        self.textField.backgroundColor = UIColor(hex: 0xF7F8FB)
        self.textField.layer.cornerRadius = 4
        self.textField.clipsToBounds = true
        self.textField.textAlignment = .center
//        self.textField.returnKeyType = .search
//        self.textField.keyboardType = .default
//        self.textField.clearButtonMode = .always
        self.textField.delegate = self
//        self.textField.addTarget(self , action: #selector(self.textFieldChanged(textField:)), for: UIControlEvents.editingChanged)
        self.contentView.addSubview(self.textField)
        
        self.subtractBtn = UIButton(frame: CGRect(x: 0, y: 0, width: 24, height: 24))
        self.subtractBtn.backgroundColor = UIColor(hex: 0xAFBED7,alpha: 0.3)
        self.subtractBtn.setTitle("-", for: .normal)
        self.subtractBtn.titleLabel?.font = UIFont(name: Theme.PingFangSCRegular, size: 24)
        self.subtractBtn.setTitleColor(UIColor(hex: 0xAFBED7), for: .normal)
        self.subtractBtn.addTarget(self, action: #selector(subtractBtnAntion), for: .touchUpInside)
        self.subtractBtn.layer.cornerRadius = 12
        self.subtractBtn.clipsToBounds = true
        self.subtractBtn.titleEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 3, right: 0)
        self.subtractBtn.contentVerticalAlignment = .center
        self.contentView.addSubview(self.subtractBtn)
        
        self.addBtn = UIButton(frame: CGRect(x: 0, y: 0, width: 24, height: 24))
        self.addBtn.backgroundColor = UIColor(hex: 0xAFBED7,alpha: 0.3)
        self.addBtn.setTitle("+", for: .normal)
        self.addBtn.titleEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 3, right: 0)
        
        self.addBtn.titleLabel?.font = UIFont(name: Theme.PingFangSCRegular, size: 24)
        self.addBtn.setTitleColor(Theme.titleLabColor, for: .normal)
        self.addBtn.layer.cornerRadius = 12
        self.addBtn.clipsToBounds = true
        self.addBtn.contentVerticalAlignment = .center
        self.addBtn.addTarget(self, action: #selector(addBtnAntion), for: .touchUpInside)
        self.contentView.addSubview(self.addBtn)
        
        
        
        self.rigthLab = UILabel(frame: CGRect(x: 18, y: 0, width: 100, height: 52))
        self.rigthLab.font = UIFont.init(name: Theme.PingFangSCRegular, size: 14)
        self.rigthLab.textColor = UIColor(hex: 0xAFBED7)
        self.rigthLab.textAlignment = .right
        self.contentView.addSubview(self.rigthLab)
        
        
        self.line = UIView(frame: CGRect(x: 18, y: self.leftLab.bottom, width: kScreenWidth - 36, height: 0.5))
        self.line.backgroundColor = UIColor.init(hex: 0x939AA8,alpha: 0.12)
        self.contentView.addSubview(self.line)
        
    }
    
    func addLayout() {
        
        self.leftLab.snp.makeConstraints { make in
            make.top.equalTo(11)
            make.left.equalTo(14)
            make.height.equalTo(30)
            make.bottom.equalTo(-11)
        }
        
        
        self.questionBtn.snp.makeConstraints { make in
            make.top.equalTo(11)
            make.left.equalTo(self.leftLab.snp.right).offset(4)
            make.width.equalTo(30)
            make.height.equalTo(30)
        }
        
       
        
        self.textField.snp.makeConstraints { make in
            make.width.equalTo(74)
            make.right.equalTo(-66)
            make.centerY.equalTo(self.contentView.snp.centerY)
            make.height.equalTo(38)
        }
        
        
        self.addBtn.snp.makeConstraints { make in
            make.centerY.equalTo(self.textField.snp.centerY)
            make.height.equalTo(24)
            make.width.equalTo(24)
            make.left.equalTo(self.textField.snp.right).offset(10)
        }
        
        self.subtractBtn.snp.makeConstraints { make in
            make.centerY.equalTo(self.textField.snp.centerY)
            make.height.equalTo(24)
            make.width.equalTo(24)
            make.left.equalTo(self.textField.snp.left).offset(-34)
        }
        
        
        
        self.line.snp.makeConstraints { make in
            make.left.equalTo(14)
            make.right.equalTo(-14)
            make.height.equalTo(1)
            make.bottom.equalTo(-1)
        }
        
        
        self.rigthLab.snp.makeConstraints { make in
            make.top.equalTo(11)
            make.right.equalTo(-14)
            make.height.equalTo(30)
            make.bottom.equalTo(-11)
        }
      
    }
    
    
    //model
    func setContentModel(contentModel: CarDetailsContentModel) {
        self.contentModel = contentModel
        self.leftLab.text = MMLocalizedString("interest_rate", comment: "利率")
        self.rigthLab.text =  "%"
        self.rigthLab.textColor = Theme.titleLabColor
        self.textField.text = "\(contentModel.interestRate.cleanZero)"
        self.updateConstraints()
        
    }
    
    
    
    //MARk: - Antion
   @objc func questionBtnAntion() {
       
       let parantView = self.viewController().view
       guard let toPoint = parantView?.convert(self.questionBtn.center, fromViewOrWindow: self) else { return  }
       let overView = WBPopOverView.init(origin: toPoint, width: kScreenWidth - self.questionBtn.width - 100, height: 89, direction: WBArrowDirection.up1)
       overView?.backView.backgroundColor = UIColor(hex: 0x222222)
       overView?.backView.layer.cornerRadius = 8
       overView?.backView.layer.masksToBounds = true
       let contentLab = UILabel(frame: CGRect(x: 18, y: 20, width: overView!.width - 31, height: 52))
       contentLab.font = UIFont.init(name: Theme.PingFangSCRegular, size: 13)
       contentLab.textColor = .white
       contentLab.numberOfLines = 0
       contentLab.text = MMLocalizedString("interest_rate_explain", comment: "计为利息的贷款比例是以贷款额的实际年利率（APR）表示。")
       
       overView?.backView.addSubview(contentLab)
       overView?.pop()
       
   }
    
    @objc func subtractBtnAntion(){
        if self.contentModel?.interestRate ?? 0 > 0{
            self.contentModel?.interestRate = (self.contentModel.interestRate - 0.25)
        }else{
            self.contentModel?.interestRate = 0
        }
        self.delegate?.selectInterestRate(rate: self.contentModel.interestRate)
    }
    
    
    @objc func addBtnAntion(){
        if self.contentModel?.interestRate ?? 0 < 100{
            self.contentModel?.interestRate = (self.contentModel.interestRate + 0.25)
        }else{
            self.contentModel?.interestRate = 0
        }
        self.delegate?.selectInterestRate(rate: self.contentModel.interestRate)
    }
    
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}
extension InterestRateTableViewCell: UITextFieldDelegate{
    func textFieldDidEndEditing(_ textField: UITextField) {
        
        if textField.text ?? "" < "0"{
            self.contentModel?.interestRate = 0
        }
        if textField.text ?? "" > "100"{
            self.contentModel?.interestRate = 100
        }
        
        self.delegate?.selectInterestRate(rate: self.contentModel.interestRate)
    }
}
